package com.itheima.service;


import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class ServiceTest {

    @Test
    public void findById(){
       //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2. 通过sqlSession获取StudentMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //3. 调用mapper的方法
        User u = mapper.findById(1);
        System.out.println(u);

        //4. 释放资源
        sqlSession.close();
    }



    @Test
    public void add(){
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2. 通过sqlSession获取StudentMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //3. 调用mapper的方法
        User u = new User();
        u.setName("那岩");
        u.setGender("35");
        mapper.add(u);

        //4. 释放资源
        sqlSession.close();
    }


    @Test
    public void findAll(){
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2. 通过sqlSession获取StudentMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //3. 调用mapper的方法
        List<User> users = mapper.findAll();

        for (User user : users) {
            System.out.println(user);
        }

        //4. 释放资源
        sqlSession.close();
    }


    @Test
    public void findByIdOrName(){
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2. 通过sqlSession获取StudentMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //3. 调用mapper的方法
        List<User> users = mapper.findByIdOrName(3, "钟文泽");
        for (User u : users) {
            System.out.println(u);
        }

        //4. 释放资源
        sqlSession.close();
    }


    @Test
    public void delete(){
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2. 通过sqlSession获取StudentMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //3. 调用mapper的方法
        Integer result = mapper.delete(4);
        System.out.println(result);

        //4. 释放资源
        sqlSession.close();
    }


    @Test
    public void update(){
    User u=new User();
    u.setId(2);
    u.setName("二号");
    u.setGender("女");
    SqlSession sqlSession=MybatisUtils.getSqlSession();
    UserMapper mapper=sqlSession.getMapper(UserMapper.class);
    mapper.update(u);


    }


    @Test
    public void findByCondition() {
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //1. 获取Mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //2. 调用代理对象方法
        User u = new User();
        //u.setId(1);
        u.setName("王五");
       // u.setGender("45");
        User user = mapper.findByCondition(u);
        System.out.println(user);

    }


    @Test
    public void findByCondition2() {
        //1. 获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //1. 获取Mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //2. 调用代理对象方法
        User u = new User();
        //u.setId(1);
        //u.setName("王五");
        u.setGender("45");
        User user = mapper.findByCondition2(u);
        System.out.println(user);

    }







}
